package com.example.sharpshopdemo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.sharpshopdemo.entity.Result;
import com.example.sharpshopdemo.entity.User;
import com.example.sharpshopdemo.mapper.UserMapper;
import com.example.sharpshopdemo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl
        extends ServiceImpl<UserMapper, User>
        implements UserService {

    @Autowired
    private Result result;

    @Override
    public Result getAll() {


        result.setSuccess(true);
        result.setData(this.list());

        return result;
    }

    //增加
    @Autowired
    private UserMapper userMapper;

    public Result insertData(User user) {
        boolean success = false;
        success = userMapper.insert(user) > 0 ? true : false;
        result.setSuccess(success);
        return result;
    }

    //修改

    public Result updateData(User user) {
        boolean success = false;
        // 注意：这里的"updatData"可能是拼写错误，应为"updateData"
        success = userMapper.updateById(user) > 0 ? true : false;
        result.setSuccess(success);
        return result;
    }

    //删除

    public Result deleteData(User user) {
        boolean success = false;
        success = userMapper.deleteById(user) > 0 ? true : false;
        result.setSuccess(success);
        return result;
    }


    //查询
    public User getUserById(Long id) {
        return userMapper.selectById(id);
    }

    public List<User> getAllUsers() {
        return userMapper.selectList(null);
    }

    public User getUserByName(String name) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", name);
        return userMapper.selectOne(queryWrapper);
    }
}
